/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package candidate.ejb;

import candidate.beans.NewEditCandidateBean;
import entities.Candidate;
import entities.CandidateLanguageInfo;
import facades.CandidateFacade;
import facades.CandidateLanguageInfoFacade;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.primefaces.model.UploadedFile;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;
import qualifiers.EditedEntityEvent;
import util.ImageStoreHelper;
import static util.ImageStoreHelper.maxPixels;
import static util.ImageStoreHelper.pathToTempDirectory;
import videoProcessing.FFMPEGVideoResizer;
import videoProcessing.VideoResizer;

/**
 *
 * @author Alvaro
 */
@Stateless
public class VideoProcesingEjb {
    @Inject
    private CandidateFacade candidateFacade;
    @Inject
    @EditedEntityEvent
    private Event<Candidate> updatedCandidateEvent;
    

    
    @Asynchronous
    public void storeTemporaryVideo(UploadedFile videoFile,Candidate candidate,CandidateLanguageInfo candidateLanguageInfo,CandidateLanguageInfoFacade candidateLanguageInfoFacade) throws FileNotFoundException, IOException{
        try{
            Long mil = new Date().getTime();
            UUID uuid=java.util.UUID.randomUUID();
            //String videoN=(mil+videoFile.getFileName());
            String videoN1=String.valueOf(mil);
            String videoN2=String.valueOf(uuid.getLeastSignificantBits());
            String videoN=videoN1+videoN2;
            String videoName=videoN.replaceAll(" ", "");
            File file=new File(ImageStoreHelper.pathToTempDirectory+videoName);
            byte[] bytes = IOUtils.toByteArray(videoFile.getInputstream());
            org.apache.commons.io.FileUtils.writeByteArrayToFile(file, bytes);
            VideoResizer resizer=new FFMPEGVideoResizer();
            Long milResized = new Date().getTime();
            resizer.getMp4Video(pathToTempDirectory+videoName, ImageStoreHelper.pathToPersistentStore+candidate.getIdCandidate().toString()+"/"+"videos/"+milResized+".mp4", maxPixels);
            resizer.getOggVideo(pathToTempDirectory+videoName, ImageStoreHelper.pathToPersistentStore+candidate.getIdCandidate().toString()+"/"+"videos/"+milResized+".ogg", maxPixels);
            resizer.getWebmVideo(pathToTempDirectory+videoName, ImageStoreHelper.pathToPersistentStore+candidate.getIdCandidate().toString()+"/"+"videos/"+milResized+".webm", maxPixels);
            //file.delete();
            candidateLanguageInfo.setVideo(milResized.toString());
            candidateLanguageInfoFacade.edit(candidateLanguageInfo);
            Date date = new Date();
            candidate.getUser().setLastDate(date);
            candidate.setLastModificationDate(date);
            candidateFacade.edit(candidate);
            updatedCandidateEvent.fire(candidate);
            PushContext pushContext = PushContextFactory.getDefault().getPushContext();
            String channel="/videoUpdated/"+candidate.getIdCandidate().toString();
            pushContext.push(channel, candidate.getIdCandidate().toString());
        }
        catch(Exception e){
            candidateLanguageInfo.setVideo(null);
            candidateFacade.edit(candidate);
            PushContext pushContext = PushContextFactory.getDefault().getPushContext();
            String channel="/videoException/"+candidate.getIdCandidate().toString();
            pushContext.push(channel, candidate.getIdCandidate().toString());
        }
    }
    
}
